package com.example.reagent_management.bindadapter;

import android.annotation.SuppressLint;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toolbar;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.reagent_management.R;
import com.google.android.material.bottomnavigation.BottomNavigationItemView;
import com.google.android.material.bottomnavigation.BottomNavigationMenuView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

import java.util.List;

import androidx.constraintlayout.motion.widget.MotionLayout;
import androidx.databinding.BindingAdapter;
import androidx.navigation.NavController;
import androidx.navigation.ui.NavigationUI;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

public class CustomBindAdapter {
    private static final String TAG = "CustomBindAdapter";

    //BottomNavigationView 相关
    @BindingAdapter(value = "setController")
    public static void setUpWithController(BottomNavigationView navigationMenuView, NavController nav ){
        if (nav != null)
            NavigationUI.setupWithNavController(navigationMenuView,nav);
        navigationMenuView.setLongClickable(false);
    }
    @BindingAdapter(value = { "setListen"},requireAll = false)
    public static void setListener(BottomNavigationView bottomNavigationView, NavigationBarView.OnItemSelectedListener listener){
        if (listener != null)
        bottomNavigationView.setOnItemSelectedListener(listener);
        bottomNavigationView.setLongClickable(false);
    }

    /**
     * 设置红点
     * @param mBottomNavigationView
     * @param mPosition
     * @param mCount
     */
    @SuppressLint("RestrictedApi")
    @BindingAdapter(value = {"setItemNumPosition","setItemNumCount"},requireAll = true)
    public static  void displayItemNum(final BottomNavigationView mBottomNavigationView, final int mPosition, final long mCount) {
//        如果你显示超多4个的话建议限制icon大小
//        app:itemIconSize="@dimen/dp_20"
//        这个属性也很总要
//        app:labelVisibilityMode="labeled"
        try {
            final int maxSize = mBottomNavigationView.getItemIconSize();
            if (mPosition > maxSize || mPosition < 0) {
                return;
            }
            //Acquisition child View BottomNavigationMenuView Objects
            final BottomNavigationMenuView menuView = (BottomNavigationMenuView) mBottomNavigationView.getChildAt(0);
            final View mTab = menuView.getChildAt(mPosition - 1);
            final BottomNavigationItemView itemView = (BottomNavigationItemView) mTab;
            View mBadge = itemView.getChildAt(3);
            if (mBadge == null) {
                //避免重复添加创建
                mBadge = LayoutInflater.from(mBottomNavigationView.getContext()).inflate(R.layout.message_badge, menuView, false);
                itemView.addView(mBadge);
            }
            final TextView mTv = mBadge.findViewById(R.id.tv_msg_count);
            if (mCount <= 0 ) {
                mTv.setVisibility(View.GONE);
                mTv.setText(String.format("%s", mCount));
            } else {
                mTv.setVisibility(View.VISIBLE);
                if (mCount > 99) {
                    mTv.setText(String.format("%s+", 99));
                } else {
                    mTv.setText(String.format("%s", mCount));
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "displayItemNum: "+e.toString() );
            e.printStackTrace();
        }
    }

    /*======================================================================*/
    @BindingAdapter(value = {"setAdapter"},requireAll = false)
    public static void setAdapter(ViewPager2 viewPager2, FragmentStateAdapter adapter){
        if (adapter != null){
            viewPager2.setOffscreenPageLimit(4);
            viewPager2.setUserInputEnabled(false);
            viewPager2.setAdapter(adapter);
        }
    }

    @BindingAdapter(value = {"setCurrentPage"},requireAll = false)
    public static void setCurrentPage(ViewPager2 viewPager2,int id){
        if (viewPager2.getAdapter() != null){
            if (viewPager2.getCurrentItem() != id)
                viewPager2.setCurrentItem(id,false);
        }
    }

    /*===============================================*/
    //RecycleView 相关
    @BindingAdapter(value = {"setRecycleAdapter"},requireAll = false)
    public static void setRecycle(RecyclerView recyclerView, RecyclerView.Adapter adapter){
        if (adapter != null)
        recyclerView.setAdapter(adapter);
    }

    @SuppressLint("NotifyDataSetChanged")
    @BindingAdapter(value = {"submitLIst"},requireAll = false)
    public static void setList(RecyclerView recyclerView, List list){
        if (recyclerView.getAdapter() != null && list != null)
        if (recyclerView.getAdapter() instanceof BaseQuickAdapter){
//            ((BaseQuickAdapter<?, ?>) recyclerView.getAdapter()).setDiffNewData(list);
            ((BaseQuickAdapter<?, ?>) recyclerView.getAdapter()).setList(list);
        }else {
            Log.d(TAG, "setList: 不是基于BaseQuickAdapter");
        }
    }
    /*===============================================*/
    //ToolBar
    @BindingAdapter(value = {"initToolBar","clickListen"},requireAll = false)
    @SuppressLint("UseCompatLoadingForDrawables")
    public static void init(Toolbar toolbar, boolean hasIcon, View.OnClickListener listener){
        if (hasIcon){
            toolbar.setNavigationIcon(toolbar.getResources().getDrawable(R.drawable.ic_baseline_arrow_back_24));
            if (listener != null)
            toolbar.setNavigationOnClickListener(listener);
        }
    }

    //设置ViewGroup或view
    @BindingAdapter(value = {"setGone"},requireAll = false)
    public static void setGone(ViewGroup viewGroup,Boolean gone){
        if (gone != null){
            if (gone){
                viewGroup.setVisibility(View.INVISIBLE);
            }else {
                viewGroup.setVisibility(View.VISIBLE);
            }
        }
    }

    @BindingAdapter(value = {"setClickListener"},requireAll = false)
    public static void setListener(View button, View.OnClickListener listener){
        if (listener != null)
        button.setOnClickListener(listener);
    }

}
